﻿using System;
using System.Linq;
using System.Security.Cryptography;
using System.ServiceProcess;
using System.Threading;
using DataProcessor.EF;
using DataProcessor.Process;
using Hjt212Parser;
using log4net;

namespace DataProcessor
{
    public partial class ServiceProcessor : ServiceBase
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ServiceProcessor));

        public ServiceProcessor()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {

                //Util.Init();
                new Thread(ProcessDataMsg).Start();

                new Thread(Downline).Start();
                Log.Info("完成启动服务...");
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }


        }
        private void Downline(object state)
        {
            while (true)
            {
                Process.Downline.Downline3012();
                Process.Downline.Downline3012Helper();
                Process.Downline.Downline3022();
                Process.Downline.Downline3097();
                Process.Downline.Downline5012();
                Process.Downline.Downline5013();
                ThreadUtil.Sleep(1000);
            }
        }

        private void Downline_2(object state)
        {
            while (true)
            {
                ThreadUtil.Sleep(5 * 60 * 1000);
                Process.Downline.Downline5012_2();
                Process.Downline.Downline5013_2();
            }
        }
        protected override void OnStop()
        {
            Log.Info("The processor Stoped!");
            Environment.Exit(0);
        }


        private void ProcessDataMsg(object state)
        {
            while (true)
            {
                ProcessMsg();
                ThreadUtil.Sleep(100);
            }
        }
      

        private static void ProcessMsg()
        {
            try
            {
                using (var gCtx = new GatewayContext())
                {
                    var notProcessed = "NotProcessed";
                    var items =
                        gCtx.received_msg.Where(t => t.is_length_right)
                            .Where(t => t.processed_flag.Contains(notProcessed))
                            .OrderBy(t => t.id)
                            .Take(1000)
                            .ToList();
                    if (items.Count > 0)
                        Log.Info($"Begin processing message num is {items.Count}.");
                    foreach (var item in items)
                    {
                        Log.Info("Processing:" + Environment.NewLine + item.received_string);
                        item.processed_flag = "ProcessingFinished";
                        switch (item.cn)
                        {
                            case "2011":
                            case "G011":
                                ProcessorMonitor.Process(item.received_string);
                                DataInstructionProcessor.Process2011(item.received_string);
                                break;
                            case "2051":
                            case "G051":
                                ProcessorMonitor.Process(item.received_string);
                                DataInstructionProcessor.Process2051(item.received_string);
                                break;
                            case "2061":
                            case "G061":
                                ProcessorMonitor.Process(item.received_string);
                                DataInstructionProcessor.Process2061(item.received_string);
                                break;
                            case "2031":
                            case "G031":
                                ProcessorMonitor.Process(item.received_string);
                                DataInstructionProcessor.Process2031(item.received_string);
                                DataInstructionProcessor.Process2031Month(item.received_string);
                                DataInstructionProcessor.Process2031Year(item.received_string);
                                break;
                            case "9011":
                                ResponseProcessor.Process9011(item.received_string);
                                break;
                            case "9012":
                                ResponseProcessor.Process9012(item.received_string);
                                break;
                            case "9014":
                                ResponseProcessor.Process9014(item.received_string);
                                break;
                            case "3097":
                                CmdInstructionProcessor.Process3097(item.received_string);
                                break;
                            case "3071":
                                CmdInstructionProcessor.Process3071(item.received_string);
                                break;

                        }
                        if (item.cn.Contains("0000")|| item.cn.Contains("G000"))
                        {
                            ProcessorStatus.ProcessOffline(item.received_string);
                        }
                        else
                        {
                            //if (item.cn.Contains("2011") || item.cn.Contains("2051") || item.cn.Contains("2061") || item.cn.Contains("2031"))
                                ProcessorStatus.ProcessOnline(item.received_string);
                        }
                        Log.Info("Processing finish.");
                    }
                    gCtx.SaveChanges();
                    if (items.Count > 0)
                        Log.Info($"Finish processing message num is {items.Count}.");
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

    }
}
